package ge.telasi.tasks.ui.structure;

import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.model.validation.StructureValidator;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import javax.swing.JComponent;
import javax.swing.JPanel;
import static ge.telasi.tasks.ui.log.LoggerUtils.clearLogger;

/**
 * @author dimitri
 */
public class StructurePanel extends JPanel implements Loggable {

    public StructurePanel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lblType = new javax.swing.JLabel();
        lblParent = new javax.swing.JLabel();
        cmbType = new ge.telasi.tasks.ui.structure.StructureTypeCombo();
        txtName2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        structureSelector1 = new ge.telasi.tasks.ui.structure.StructureSelector();

        lblName.setText("დასახელება:");

        txtName.setName("Structure.name"); // NOI18N

        lblType.setText("ერთეულის ტიპი:");

        lblParent.setText("ზედა სტრუქტურა:");

        cmbType.setName("Structure.type"); // NOI18N

        txtName2.setName("Structure.name2"); // NOI18N

        jLabel1.setText("დასახელება 2:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblName)
                    .addComponent(lblType)
                    .addComponent(jLabel1)
                    .addComponent(lblParent))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                    .addComponent(txtName2, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                    .addComponent(structureSelector1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(structureSelector1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblParent))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ge.telasi.tasks.ui.structure.StructureTypeCombo cmbType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblParent;
    private javax.swing.JLabel lblType;
    private ge.telasi.tasks.ui.structure.StructureSelector structureSelector1;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtName2;
    // End of variables declaration//GEN-END:variables

    private Logger logger;
    private Structure parent;
    private Structure structure;

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] { txtName, txtName2, cmbType };
    }

    public Structure getParentStructure() {
        return parent;
    }

    public void setParentStructure(Structure parent) {
        this.parent = parent;
        if (parent != null) {
            setStructure(null);
        }
    }

    public Structure getStructure() {
        Structure formStructure = new Structure();
        if (structure != null) {
            formStructure.setId(structure.getId());
        }
        formStructure.setParent(structureSelector1.getStructure());
        formStructure.setName(txtName.getText());
        formStructure.setName2(txtName2.getText());
        formStructure.setType(cmbType.getType());
        return formStructure;
    }

    public void setStructure(Structure structure) {
        this.structure = structure;
        if (structure == null) {
            txtName.setText("");
            txtName2.setText("");
            cmbType.setType(-1);
            structureSelector1.setStructure(parent);
        } else {
            txtName.setText(structure.getName());
            txtName2.setText(structure.getName2());
            cmbType.setType(structure.getType());
            structureSelector1.setStructure(structure.getParent());
        }
    }

    public boolean validateForm() {
        clearLogger(logger, allFields());
        Structure formStructure = getStructure();
        try {
            new StructureValidator().validateFull(formStructure);
            return true;
        } catch (TaskException ex) {
            LoggerUtils.manageException(this, logger, ex, allFields());
            return false;
        }

    }

}
